<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false" width="60%" top="70px">
    <!-- <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" -->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="活动名称" prop="title">
            <el-input v-model="dataForm.title"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动开始时间" prop="startTime">
            <el-date-picker style="width: 55%; margin-right: 3%" v-model="date1" type="date" value-format="yyyy-MM-dd"
              placeholder="选择日期时间">
            </el-date-picker>
            <el-time-picker style="width: 42%" v-model="value1" placeholder="时间点" value-format="HH:mm" format="HH:mm">
            </el-time-picker>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="活动结束时间" prop="endTime">
            <el-date-picker style="width: 55%; margin-right: 3%" v-model="date2" type="date" value-format="yyyy-MM-dd"
              placeholder="选择日期时间">
            </el-date-picker>
            <el-time-picker style="width: 42%" v-model="value2" placeholder="时间点" value-format="HH:mm" format="HH:mm">
            </el-time-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动类型" prop="activityType">
            <ren-select v-model="dataForm.activityType" dict-type="activityType" placeholder="请选择"
              style="width: 100%"></ren-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否需要报名" prop="isApplication">
            <el-radio-group v-model="dataForm.isApplication">
              <el-radio :label="0">否</el-radio>
              <el-radio :label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <template v-if="dataForm.isApplication == 1">
          <el-col :span="12">
            <el-form-item label="报名开始时间" prop="applicationStart">
              <el-date-picker style="width: 55%; margin-right: 3%" v-model="date3" type="date" value-format="yyyy-MM-dd"
                placeholder="选择日期时间">
              </el-date-picker>
              <el-time-picker style="width: 42%" v-model="value3" placeholder="时间点" value-format="HH:mm" format="HH:mm">
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报名结束时间" prop="applicationEnd">
              <el-date-picker style="width: 55%; margin-right: 3%" v-model="date4" type="date" value-format="yyyy-MM-dd"
                placeholder="选择日期时间">
              </el-date-picker>
              <el-time-picker style="width: 42%" v-model="value4" placeholder="时间点" value-format="HH:mm" format="HH:mm">
              </el-time-picker>
            </el-form-item>
          </el-col>
        </template>
        <el-col :span="12">
          <el-form-item label="所属区域" prop="islandId">
            <el-select v-model="dataForm.islandId" placeholder="所属区域" clearable style="width: 100%">
              <el-option v-for="item in scenicOptions" :key="item.id" :label="item.scenicName" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="地址" prop="address">
            <el-input v-model="dataForm.address"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <p class="pdl60">
            <span style="color: #f56c6c; margin-right: 4px">*</span>
            活动封面图<span>(支持png、jpg，建议尺寸288*288px，最多1张)</span>
          </p>
        </el-col>
        <el-col :span="24">
          <el-form-item label="" prop="pic">
            <el-upload class="avatar-uploader" :action="url" :show-file-list="false" :on-success="(res, file, fileList) => handleAvatarSuccess(res, file, 'pic')
              " :before-upload="(file) => beforeUploadHandle(file, 'pic')">
              <img v-if="dataForm.pic" :src="dataForm.pic" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="内容" prop="content">
            <editor-vue style="margin-top: -30px" :content="dataForm.content" @EditorChange="getEditor"
              ref="infoIntroduceRef"></editor-vue>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
    <!-- 裁剪框 -->
    <cropper-dlg v-if="showCropper" :dialogVisible.sync="showCropper" :img-file="currentFile" ref="vueCropper"
      @upload="cropperUpload"></cropper-dlg>
  </el-dialog>
</template>

<script>
import Cookies from "js-cookie";
import debounce from "lodash/debounce";
import editorVue from "@/components/editor";
export default {
  components: {
    editorVue,
  },
  data() {
    return {
      url: `${window.SITE_CONFIG["apiURL"]
        }/oss/file/aliyunUpload?access_token=${Cookies.get("access_token")}`,
      num: 0,
      fileList: [],
      visible: false,
      dataForm: {
        islandId: '',
        id: "",
        isApplication: "",
        applicationEnd: "",
        applicationStart: "",
        content: "",
        title: "",
        pic: "",
        pics: [],
        startTime: "",
        endTime: "",
        address: "",
        rule: "",
        addressType: "",
        activityType: "",
      },
      scenicOptions: [],
      value1: "",
      date1: "",
      value2: "",
      date2: "",
      value3: "",
      date3: "",
      value4: "",
      date4: "",
      currentFile: "",
      cropperIndex: null,
      showCropper: false, //裁剪弹框
      isCropper: true, //上传之前是否显示裁剪
    };
  },
  computed: {
    dataRule() {
      return {
        title: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        startTime: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        endTime: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        activityType: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        isApplication: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        applicationStart: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        islandId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        applicationEnd: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        address: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        pic: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        content: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  watch: {
    date1(newval, oldval) {
      if (newval != oldval && this.value1 && newval) {
        this.dataForm.startTime = newval + " " + this.value1;
      } else {
        this.dataForm.startTime = '';
      }
    },
    date2(newval, oldval) {
      if (newval != oldval && this.value1 && newval) {
        this.dataForm.endTime = newval + " " + this.value2;
      } else {
        this.dataForm.endTime = '';
      }
    },
    value1(newval, oldval) {
      if (newval != oldval && this.date1 && newval) {
        this.dataForm.startTime = this.date1 + " " + newval;
      } else {
        this.dataForm.startTime = '';
      }
    },
    value2(newval, oldval) {
      if (newval != oldval && this.date2 && newval) {
        this.dataForm.endTime = this.date2 + " " + newval;
      } else {
        this.dataForm.endTime = '';
      }
    },
    date3(newval, oldval) {
      if (newval != oldval && this.value3 && newval) {
        this.dataForm.applicationStart = newval + " " + this.value3;
      } else {
        this.dataForm.applicationStart = '';
      }
    },
    date4(newval, oldval) {
      if (newval != oldval && this.value4 && newval) {
        this.dataForm.applicationEnd = newval + " " + this.value4;
      } else {
        this.dataForm.applicationEnd = '';
      }
    },
    value3(newval, oldval) {
      if (newval != oldval && this.date3 && newval) {
        this.dataForm.applicationStart = this.date3 + " " + newval;
      } else {
        this.dataForm.applicationStart = '';
      }
    },
    value4(newval, oldval) {
      if (newval != oldval && this.date4 && newval) {
        this.dataForm.applicationEnd = this.date4 + " " + newval;
      } else {
        this.dataForm.applicationEnd = '';
      }
    },
  },
  methods: {
    getEditor(val) {
      this.dataForm.content = val;
    },
    init() {
      this.visible = true;
      this.getScenicOptions();
      this.$nextTick(() => {
        this.date1 = "";
        this.date2 = "";
        this.value1 = "";
        this.value2 = "";
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        } else {
          this.$nextTick(() => {
            this.$refs.infoIntroduceRef.init();
          });
        }
      });
    },
    getScenicOptions() {
      this.$http
        .get("scenic/isscenic/selectScenicList", {})
        .then(({ data: res }) => {
          if (res.msg == "success") {
            this.scenicOptions = res.data;
          }
        });
    },
    //获取信息
    getInfo() {
      this.$http
        .get("/scenic/commactivity/" + this.dataForm.id)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
          if (this.dataForm.startTime) {
            let time = this.dataForm.startTime.split(" ");
            this.date1 = time[0];
            this.value1 = time[1];
          }
          if (this.dataForm.endTime) {
            let time2 = this.dataForm.endTime.split(" ");
            this.date2 = time2[0];
            this.value2 = time2[1];
          }
          if (this.dataForm.applicationStart) {
            let time3 = this.dataForm.applicationStart.split(" ");
            this.date3 = time3[0];
            this.value3 = time3[1];
          }
          if (this.dataForm.applicationEnd) {
            let time4 = this.dataForm.applicationEnd.split(" ");
            this.date4 = time4[0];
            this.value4 = time4[1];
          }
          this.$nextTick(() => {
            this.$refs.infoIntroduceRef.init();
          });
        })
        .catch(() => { });
    },
    // 上传之前
    beforeUploadHandle(file, name) {
      if (
        file.type !== "image/jpg" &&
        file.type !== "image/jpeg" &&
        file.type !== "image/png" &&
        file.type !== "image/gif"
      ) {
        this.$message.error(
          this.$t("upload.tip", {
            format: "jpg、png、gif",
          })
        );
        return false;
      }
      if (this.isCropper) {
        //显示裁剪
        this.currentFile = file;
        this.cropperIndex = name;
        this.showCropper = true;
        this.$nextTick(() => {
          this.$refs.vueCropper.init();
        });
        return false;
      }
    },
    cropperUpload(data) {
      this.showCropper = false;
      if (data != "close") {
        //裁剪成功后获取的图片
        this.$http.post("/oss/file/aliyunUpload", data).then((res) => {
          if (res.data.code == 0) {
            this.handleAvatarSuccess(res.data, "", this.cropperIndex);
          }
        });
      }
    },
    handleAvatarSuccess(res, file, key) {
      this.dataForm[key] = res.data.url;
    },
    // 上传成功
    successHandle1(res, file) {
      if (res.code !== 0) {
        return this.$message.error(res.msg);
      }
      this.dataForm.pic = res.data.url;
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.dataForm.startTime = this.date1 + " " + this.value1;
          this.dataForm.endTime = this.date2 + " " + this.value2;
          this.dataForm.applicationStart = this.date3 + " " + this.value3;
          this.dataForm.applicationEnd = this.date4 + " " + this.value4;
          if (!this.dataForm.isApplication) {
            this.dataForm.applicationStart = "";
            this.dataForm.applicationEnd = "";
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/scenic/commactivity",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => { });
        });
      },
      1000,
      {
        leading: true,
        trailing: false,
      }
    ),
  },
};
</script>

<style lang="scss" scoped>
::v-deep.el-time-spinner__wrapper {
  width: 50% !important;

  &:last-child {
    display: none;
  }
}

.txt {
  line-height: 40px;
  height: auto;
  margin: 0;
  padding: 0;

  &.content {
    ::v-deep p {
      padding: 0;
      line-height: 24px;
      margin: 0 !important;
    }

    ::v-deep img {
      width: 100%;
    }

    img {
      width: 100%;
    }
  }
}
</style>
